<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0;">
      <el-form :inline="true" :model="roleQuery">
        <el-form-item>
          <el-input v-model="roleQuery.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;margin-bottom: 20px;"
              border default-expand-all>
      <el-table-column type="selection" width="100">
      </el-table-column>
      <el-table-column type="index" width="100">
      </el-table-column>
      <el-table-column prop="name" label="角色" >
      </el-table-column>
      <el-table-column prop="sn" label="标识" width="240">
      </el-table-column>
      <el-table-column label="操作" width="240">
        <template scope="scope">
          <el-button size="small" type="waring" @click="handleEdit(scope.$index, scope.row)">编辑/授权</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
<!--          <el-button type="danger" size="small" @click="handlePermission(scope.$index, scope.row)">设置权限</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <!--  新增和编辑
          :visible.sync ture就打开  false就隐藏
    -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="roleForm" :rules="roleForm" ref="roleForm">
        <el-form-item label="角色" :label-width="formLabelWidth" prop="name">
          <el-input v-model="roleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="标识" :label-width="formLabelWidth" prop="sn">
          <el-input v-model="roleForm.sn"></el-input>
        </el-form-item>
        <el-table-column prop="sn" label="标识" width="240">
          <el-tree :data="permissionTree"
                   show-checkbox
                   :props="defaultProps"
                   ref="trees"
                   :default-checked-keys="checkedKeys"
                   node-key="id"
                   @node-click="handleNodeClick">

          </el-tree>
        </el-table-column>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit" :loading="addLoading">确 定</el-button>
      </div>
    </el-dialog>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          layout="total, prev, pager, next"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          :page-size="pageSize"
          :total="total"
          :current-page="currentPage"
          style="float:right;">
      </el-pagination>
    </el-col>
  </section>
</template>

<script type="text/javascript">
export default {
  data(){
    return {
      checkedKeys:[],//做权限回显的
      addLoading:false,
      roleForm:{
        id:null,
        name:"",
        sn:"",
      },
      defaultProps:{
        children:'children',
        label:'name',
      },
      permissionTree:[],//展示系统所有的权限菜单
      dialogFormVisible:false,
      title:"修改",
      pageInfo:[],
      roleQuery:{
        keyword: "",
      },
      currentPage:1,
      pageSize:5,
      listLoading:true,
      sels:[],
      total:0,
      formLabelWidth:"120px",
      roleTrees:[],
    }
  },
  methods:{
    onSubmit(){
      this.addLoading = true;
      let nodes = this.$refs.trees.getCheckedNodes();
      let parm = {
        role:this.roleForm,
        permissions:nodes
      };

      let url = "/authRole/save";
      if (this.roleForm.id){
        url="/authRole/update"
      }
      this.$confirm('确认提交吗？', '提示', {}).then(() => {
        this.$http.put(url, parm).then(res => {
          this.addLoading = false;
          if (res.data.success) {
            this.$message.success("添加操作成功！");
          }else {
            this.$message.error("添加操作失败！");
          }
          this.dialogFormVisible=false;
          this.loadListData();
        });
      }).catch(() => {
        this.addLoading = false;
        this.$message.info("取消操作");
      })
    },
    handleNodeClick(data){
      console.log(data)
    },
    loadPermissionTree(){
      this.$http.get("/authPermission/permissionTree").then(res=>{
        console.log(this.permissionTree);
        this.permissionTree = res.data.data;
        console.log("----------")
        console.log(this.permissionTree);
      })
    },
    loadListData(){
      let param={
        keyword: this.roleQuery.keyword,
        currentPage:this.currentPage,
        pageSize:this.pageSize
      };
      this.$http.post("/authRole",param).then(res=>{
        let {data,msg,success} = res.data
        if (success) {
          this.pageInfo = data.dataList;
          this.total = data.totals;
          console.log("-----=--=-=-=-=--=")
          console.log(this.pageInfo)
          console.log("--------------------------------")
          this.listLoading = false;
        }else {
          this.$message.error(msg)
        }
      })
    },
    search(){
      this.loadListData();
    },
    handleAdd(){
      //清空权限资源
      if (this.$refs.trees){
        this.$refs.trees.setCheckedKeys([]);
      }
      //清空表单
      this.roleForm={
        id: null,
        name: "",
        sn: "",
      }
      //修改标题，打开模态框
      this.title = '新增';
      //2、打开弹出框
      this.dialogFormVisible = true;
    },
    selsChange(ids){
      this.sels = ids;
    },
    loadPermissionByRoleId(id){
      this.$http.get("/authRole/permission/"+id).then(res=>{
        this.checkedKeys=res.data.data;
      })
    },
    handleEdit(index,row){
      //清空权限资源
      if (this.$refs.trees){
        this.$refs.trees.setCheckedKeys([]);
      }
      this.loadPermissionByRoleId(row.id);
      this.title = '修改';
      //数据回显
      this.roleForm=row;
      //2、打开弹出框
      this.dialogFormVisible = true;
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('此操作将永久删除该权限信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/authRole/"+row.id).then(res => {
          this.listLoading = false;
          if (res.data.success){
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }else {
            this.$message({
              type: 'error',
              message: '删除失败!'
            });
          }
          this.loadListData();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handlePermission(){

    },
    batchRemove(){
      //把对象数组转成 id数组
      let roleIds = this.sels.map(function (obj,index,array){
        return obj.id;
      })
      this.$http.delete("/authRole/deleteList",{data:roleIds}).then(res=>{
        let{msg,success} = res.data
        if (success){
          this.$message.success("删除成功！")
        }else {
          this.$message.error("操作失败！")
        }
        this.loadListData();
      })
    },
    handleCurrentChange(val){
      this.currentPage = val;
      this.loadListData();
    },
    handleSizeChange(val){
      this.pageSize = val;
      this.currentPage=1;
      this.loadListData();
    },
  },
  mounted() {
    this.loadListData();
    this.loadPermissionTree();
  }

}
</script>
<style scoped>

</style>